'use strict';

const {Controller} = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const {json} = require("../utils");
const {moveFileOrFolder, downloadQiNiuUrl, batchDownload, completelyDeleteFiles} = require("../utils/qiniuyun");
const {createReadStream} = require("fs");
const fs = require("fs");
const hall = require("../crawler/haowallpaper");
const EE = require("ee-core/ee");

/**
 * example
 * @class
 */
class CloudController extends Controller {
    cloud = Services.get('cloud')

    constructor(ctx) {
        super(ctx);
    }

    /**
     * 初始化分片上传
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */

    async uploadChunkFile(args) {
        return await this.cloud.uploadChunkFile(args)
    }

    /**
     * 开始 分片上传
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */

    async uploadChunkIndex(args) {
        return await this.cloud.uploadChunkIndex(args)
    }

    /**
     * 返回历史任务
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */

    async getHistoryTasks(args, event) {
        return await this.cloud.getHistoryTasks(args, event)
    }

    /**
     * 获取云盘文件或文件夹
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async getCloudFiles(args) {
        try {
            const {type = 'all', parent_id = null} = args.query
            if (type === 'all') {
                return json({data: await this.cloud.getAllCloudFiles(parent_id)})
            } else if (type === 'delete') {
                return json({data: await this.cloud.getDeleteCloudFiles()})
            } else {
                return json({data: await this.cloud.getTypeCloudFiles(type)})
            }
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     * 删除文件或文件夹
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async delCloudFiles(args) {
        try {
            console.log(args)
            await this.cloud.delCloudFiles(args.ids.split(','), args.del_flag)
            return json({})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     * 递归获取所有文件夹
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async getCloudAllFolder() {
        try {
            let data = await this.cloud.getCloudAllFolder()
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     * 移动选中的文件或目录到指定文件夹
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */

    async moveCloudFiles(args) {
        try {
            let data = await this.cloud.moveCloudFiles(args)
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }


    /**
     * 修改文件或文件夹
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async updateCloudFilesName(args) {
        console.log(args)
        try {
            await moveFileOrFolder(args.task)
            await this.cloud.updateCloudFilesName(args)
            return json({})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     * 下载
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async downloadQiNiuUrl(args) {
        console.log(args.query.key)
        try {
            let data = downloadQiNiuUrl(args.query.key)
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     *  批量下载
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async batchDownload(args) {
        console.log(args)
        try {
            let selectData = await this.cloud.getCloudAllFolder(args.idList, true)
            let data = await batchDownload(selectData)
            return createReadStream(data.zip)
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     *  彻底删除
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */
    async completelyDelete(args) {
        console.log(args)
        try {
            let data = await completelyDeleteFiles(args.body.fileList)
            await this.cloud.completelyDeleteFiles(args.body.fileList)
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }
    }

    /**
     * 创建文件夹或文件
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */

    async createFileOrFolder(args) {
        try {
            let {is_folder, folder_path} = args.body
            let obj
            if (is_folder == 1) {
                if (folder_path) {
                    return await this.cloud.recursiveCreate(args)
                } else {
                    obj = await this.cloud.createQiNiuFolder(args)
                }

            } else {
                obj = await this.cloud.uploadToQiNiu(args.file, args)
            }
            args.body.path = obj.url.replace('www.forgetting.ren', '')
            let data = await this.cloud.saveCloudFileData(args.body)
            return data ? json({}) : json({msg: '该文件或文件夹已存在', code: 500})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }

    }
}

CloudController.toString = () => '[class CloudController]';
module.exports = CloudController;
